<?php
/**
 * <pre>
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: x4you
 * COPYRIGHT	: 2009
 * ------------------------------------------------------
 * 
 * [GENERAL INFO OF THIS FILE] 
 * 
 * </pre>
 * 
 * Created on	:  Mar 11, 2009
 * 
 * @author		hoadn <hoadn@rubik.com.vn>
 * @access		[public/private]
 * @package	[package name]
 *
 */

defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RClassesImporter::cls( "dbo.dtos.SiteLanguageDTO" );
RClassesImporter::cls( "dbo.dtos.CountryDTO" );

class LanguageModel {

	/**
	 * Save DTO Object
	 * @param AbstractDTO $dto
	 */
	function saveDTO($dto) {
		return SysDatabase::saveDTO ( $dto );
	}
	
	/**
	 * Save the site language
	 * @param SiteLanguageDTO $dto
	 */
	function saveSiteLanguage($dto) {
		$isNew = !$dto->checkKeyNotDefault();
		// with site language we have some special treat for the label and static content
		// if new then insert more column
		if ($isNew) {
			$sql = $this->_getAlterLangColumsSQL($dto->code,  'ADD COLUMN');
		}
		else {
			$sql  = "SELECT code FROM site_language WHERE id={$dto->id}";
			$oldLang = SysDatabase::loadResult($sql);
			if ($oldLang != $dto->code) {
				$sql = $this->_getAlterLangColumsSQL($oldLang,  'DROP');
				$sql .= $this->_getAlterLangColumsSQL($dto->code,  'ADD COLUMN');
			}
		}
		
		$result = SysDatabase::saveDTO ( $dto );
		
		if ($result && $sql) {
			$db = &SysDatabase::getInstance();
			$db->setQuery($sql);
			if ($db->queryBatch(false)) {
				
			}
			else {
				$result = false;
			}
		}
		return $result;
	}
	
	function _getAlterLangColumsSQL($langCode, $alterKey = "ADD COLUMN") {
		if (strtolower($alterKey) == 'drop') {
			$type = '';
		}
		else {
			$type = 'text';
		}
		$sql = "
				ALTER TABLE `label_key` {$alterKey} `label_{$langCode}` {$type};
				ALTER TABLE `static_content` {$alterKey} `title_{$langCode}` {$type};
				ALTER TABLE `static_content` {$alterKey} `content_{$langCode}` {$type};
			";
		return $sql;
	}
	
	/**
	 * function load Event
	 *
	 * @param int $id
	 * @return SiteLanguageDTO
	 */
	function loadSiteLang( $id ) {
		$dto = new SiteLanguageDTO();
		$dto->setKeyValue($id);
		SysDatabase::fillData( $dto );
		return $dto;
	}
	
	/**
	 * Function 
	 *
	 * @param array $ids
	 * @return boolean
	 */
	function deleteSiteLang( $ids ) {
		$txIds = implode(', ', $ids);
		$sql  = "DELETE FROM site_language WHERE id IN ({$txIds})";
		$deleteObjs = SysDatabase::loadObjectList($sql);
		
		$sql = "";
		foreach ($deleteObjs as $obj) {
			$oldLang = $obj->code;
			$sql .= $this->_getAlterLangColumsSQL($oldLang,  'DROP');
		}

		$db = &SysDatabase::getInstance();
		$db->setQuery($sql);

		$result = $db->queryBatch(false);
		
		if ($result) {
			$dto	=	new SiteLanguageDTO();
			$result =  SysDatabase::execute( $dto->createDeleteSQL( $ids ) );
		}
		return $result;
	}
	
	/**
	 * function load Event
	 *
	 * @param int $id
	 * @return SiteLanguageDTO
	 */
	function loadSpokenLang( $id ) {
		$dto = new SpokenLanguageDTO();
		$dto->setKeyValue($id);
		SysDatabase::fillData( $dto );
		return $dto;
	}
	
	/**
	 * Function 
	 *
	 * @param array $ids
	 * @return boolean
	 */
	function deleteSpokenLang( $ids ) {
		$dto	=	new SpokenLanguageDTO();
		return  SysDatabase::execute( $dto->createDeleteSQL( $ids ) );
	}
	
	/**
	 * function load Event
	 *
	 * @param int $id
	 * @return SiteLanguageDTO
	 */
	function loadCountry( $id ) {
		$dto = new CountryDTO();
		$dto->setKeyValue($id);
		SysDatabase::fillData( $dto );
		return $dto;
	}
	
	/**
	 * Function 
	 *
	 * @param array $ids
	 * @return boolean
	 */
	function deleteCountry( $ids ) {
		$dto	=	new CountryDTO();
		return  SysDatabase::execute( $dto->createDeleteSQL( $ids ) );
	}
	
	/**
	 * function load Event
	 *
	 * @param int $id
	 * @return LabelKeyDTO
	 */
	function loadLabelDTO( $id ) {
		$dto = new LabelKeyDTO();
		$dto->setKeyValue($id);
		SysDatabase::fillData( $dto );
		return $dto;
	}
	
	/**
	 * Function 
	 *
	 * @param array $ids
	 * @return boolean
	 */
	function deleteLabel( $ids ) {
		$dto	=	new LabelKeyDTO();
		return  SysDatabase::execute( $dto->createDeleteSQL( $ids ) );
	}

	/**
	 * Get all code of site language
	 * @return array
	 */
	function getSiteLangCodeList() {
		$sql = "SELECT code FROM site_language";
		$res = SysDatabase::loadRowList($sql);
		return $res;
	}
}
?>